function fnUploadImagehandler(mediaUrl){
 	$j("#advertisementImage").get(0).src=__mediaPath+mediaUrl;
}

function preSaveAdvertisement(form){
	//置空无用的field
	if($j('input[name="contentType"]:checked').val() == "1"){
		$j('#url4Flash').val("");
	}else{
		$j('#url4Image').val("");
		$j('#redirectUrl').val("");
		$j('#advertisementDetail').val("");
	}
	if(!checkEndTime()){
    	return false;
    }
    var jAdPositionType = $j('input[name="adPositionTypeId"]:checked');
	if(jAdPositionType.attr("positionType") == 1){
		//置空关联目录
		$j('#relationTable').empty();
	}
	return validateAdvertisement(form);
}

function checkEndTime(){
   	if (fnCompareDate($("startPublishTime").value,$("endPublishTime").value) >0 | (fnCompareDate($("startPublishTime").value,$("endPublishTime").value) == 0 && Number($("startHour").value) >= Number($("endHour").value))){
   		$j('#endPublishTime').focus();
   		alert(__FMT.advertisement_endTimeGtStartTime);
   		return false;
   	}else{
   		return true;
   	}
}
/**
 * productAds是一个json对象，包含{categoryId,categoryName,isSubIncluded}
 */
var productAdCounter=0;//计数器
function addRelation(productAdv){
	var categoryId,categoryName,isSubIncluded;
	categoryId = productAdv.categoryId;
	categoryName = productAdv.categoryName;
	isSubIncluded = productAdv.isSubIncluded;
	
	var flag = true;
	$j('input[name="categoryIds"]').each(function(){
		if(this.value == categoryId){
			jAlert(__FMT.advertisementDetail_addCategoryAlrady);
			flag = false;
		}
	});
	if(!flag){
		return;
	}
	
	
	var html="";
	html += '<tr height="30" class="skuOptionValueClass" id="productAdv'+categoryId+'">';
	html += '<td><input type="hidden" id="categoryId'+categoryId+'" name="categoryIds"  value="'+categoryId+'" class="inputtxt"/>'+categoryName+'</td>';
	html += '<td><input type="checkbox" name="isSubIncludedChecks" id="isSubIncludedCheck'+categoryId+'"  ';
	if(isSubIncluded==1){
		html += 'checked';
	}
	html += '/>';
	html += '<input type="hidden" name="isSubIncludeds" id="isSubIncluded'+categoryId+'" value="'+isSubIncluded+'"/></td>';
	html += '<td><a href="javascript:void(0)" onclick="deleteRelation(this)" class="del"></td>';
	html += '<td></td>';
	html += '</tr>';
	
	$j('#relationTable').append(html);
	$j('#isSubIncludedCheck'+categoryId).click(function(){
			if(this.checked){
				$j('#isSubIncluded'+categoryId).val(1);
			}else{
				$j('#isSubIncluded'+categoryId).val(0);
			}
	})
	
	$j('#relationTableEmptyTr').hide();
	closeDialogOfproductCategorySelector();
}	

function deleteRelation(linkDOM){
	$j(linkDOM).parents(".skuOptionValueClass").remove();
	if($j('tr.skuOptionValueClass').length==0){
		$j('#relationTableEmptyTr').show();
	}
}
/**
 * 选择目录
 */
function selectCategoryCallback(category){
	if(category.id == 1){
		jAlert(__FMT.advertisementDetail_cannotAddRoot);
		return;
	}
	$j.getJSON(__ctxPath+"/content/advertisement/blank.html?doAction=getSimpleCategoryPathName&categoryId="+category.id,null,getSimpleCategoryPathNameCallback);
}

function getSimpleCategoryPathNameCallback(data){
	addRelation({"categoryId":data.categoryId,"categoryName":data.simpleCategoryPathName,"isSubIncluded":0});
}